package io.renren.modules.store.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.utils.ReplaceUtil;
import io.renren.modules.store.entity.TStoreDiscountDetailEntity;
import io.renren.modules.store.entity.TStoreTimeIntervalEntity;
import io.renren.modules.store.entity.vo.FoodDiscountVo;
import io.renren.modules.store.service.TStoreDiscountDetailService;
import io.renren.modules.store.service.TStoreTimeIntervalService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.store.entity.TStoreDiscountEntity;
import io.renren.modules.store.service.TStoreDiscountService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 21:22:21
 */
@RestController
@RequestMapping("store/tstorediscount")
public class TStoreDiscountController extends AbstractController {
    @Autowired
    private TStoreDiscountService tStoreDiscountService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private TStoreTimeIntervalService tStoreTimeIntervalService;
    @Autowired
    private TStoreDiscountDetailService storeDiscountDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorediscount:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        params.put("status",params.get("status"));
        PageUtils page = tStoreDiscountService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/listFoodDetail")
    @RequiresPermissions("store:tstorediscount:listFoodDetail")
    public R listFoodDetail(@RequestParam Map<String,Object> params){
        params.put("seller",getSeller());
        Page<FoodDiscountVo> foodDiscountVoPage = tStoreDiscountService.listFoodDetail(params);
        return R.ok().put("data",foodDiscountVoPage);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{discountId}")
    @RequiresPermissions("store:tstorediscount:info")
    public R info(@PathVariable("discountId") Long discountId){
		TStoreDiscountEntity tStoreDiscount = tStoreDiscountService.getById(discountId);
        String authRoleList = tStoreDiscount.getAuthRoleList();
        String timeIntervalId = tStoreDiscount.getTimeIntervalId();
        List<Long> list = new ArrayList<>();
        List<Long> list1 = new ArrayList<>();
        if (authRoleList == null){
            list.add(null);
        }
        else if (authRoleList.length() == 1){
            list.add(Long.valueOf(authRoleList));
        }else{
            String[] split = authRoleList.split(",");
            for (String s : split) {
                list.add(Long.valueOf(s));
            }
        }
        tStoreDiscount.setRoleList(list);

        if (timeIntervalId == null){
            list1.add(null);
        }
        else if (timeIntervalId.length() == 1){
            list1.add(Long.valueOf(timeIntervalId));
        }else{
            String[] split = timeIntervalId.split(",");
            for (String s : split) {
                list1.add(Long.valueOf(s));
            }
        }
        tStoreDiscount.setTimeIntervalList(list1);
        return R.ok().put("tStoreDiscount", tStoreDiscount);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorediscount:save")
    public R save(@RequestBody TStoreDiscountEntity tStoreDiscount){
        if (tStoreDiscount.getRoleList() != null){
            tStoreDiscount.setAuthRoleList(ReplaceUtil.replaceZKH(tStoreDiscount.getRoleList().toString()));
            tStoreDiscount.setSellerId(getSeller());
        }
        if (tStoreDiscount.getRoleList() != null){
            tStoreDiscount.setTimeIntervalId(ReplaceUtil.replaceZKH(tStoreDiscount.getTimeIntervalList().toString()));
            tStoreDiscount.setSellerId(getSeller());
        }
        tStoreDiscount.setSellerId(getSeller());
		tStoreDiscountService.save(tStoreDiscount);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorediscount:update")
    public R update(@RequestBody TStoreDiscountEntity tStoreDiscount){
        if (tStoreDiscount.getRoleList() != null){
            tStoreDiscount.setAuthRoleList(ReplaceUtil.replaceZKH(tStoreDiscount.getRoleList().toString()));
            tStoreDiscount.setSellerId(getSeller());
        }
        if (tStoreDiscount.getRoleList() != null){
            tStoreDiscount.setTimeIntervalId(ReplaceUtil.replaceZKH(tStoreDiscount.getTimeIntervalList().toString()));
            tStoreDiscount.setSellerId(getSeller());
        }
		tStoreDiscountService.updateById(tStoreDiscount);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorediscount:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Long[] discountIds){
		tStoreDiscountService.removeByIds(Arrays.asList(discountIds));
        storeDiscountDetailService.remove(new QueryWrapper<TStoreDiscountDetailEntity>()
            .eq("seller_id",getSeller())
                .eq("discount_id",discountIds[0])
        );
        return R.ok();
    }

}
